package gui.toolbox;

import java.util.ArrayList;

import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import model.Node;
import model.listener.CoordinateListener;
import model.listener.NodeListener;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class Chart extends JPanel implements NodeListener,
		ListSelectionListener, CoordinateListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 977616851597306802L;
	private XYSeries[][][] series;
	private XYSeriesCollection dataset = new XYSeriesCollection();
	private boolean displayX = false;
	private boolean displayY = false;
	private boolean displayZ = false;
	private ArrayList<Integer> userDisplayed = new ArrayList<Integer>();
	private int maxUsers;
	private JFreeChart chart;
	private ChartPanel chartPanel;

	public Chart(int nbrUsers) {
		super();
		maxUsers = nbrUsers;
		// Create series for each user, each coordinates and each nodes
		series = new XYSeries[Node.values().length][3][maxUsers];

		for (int i = 0; i < Node.values().length; i++) {
			for (int j = 0; j < 3; j++) {
				for (int k = 0; k < maxUsers; k++) {
					series[i][j][k] = new XYSeries("User " + k);
				}
			}
		}

		chart = ChartFactory.createXYLineChart("", "Time (ms)", "position",
				dataset, PlotOrientation.VERTICAL, true, true, true);

		chartPanel = new ChartPanel(chart);
		this.add(chartPanel);
	}

	@Override
	public void data(Node node, float x, float y, float z, int userId, int time) {

		// Check if user is inside the limit of tracking users
		if (userId < maxUsers) {
			series[node.ordinal()][0][userId].add(time, x);
			series[node.ordinal()][1][userId].add(time, y);
			series[node.ordinal()][2][userId].add(time, z);

			// add the user to the displayed info (maybe better to put it in a
			// Swing component)
			userDisplayed.add(new Integer(userId));
		}
	}

	@Override
	public void valueChanged(ListSelectionEvent e) {
		JList list = (JList) e.getSource();
		int selections[] = list.getSelectedIndices();

		dataset.removeAllSeries();

		// add series for selected items in the list
		for (int i = 0; i < selections.length; i++) {
			for (int j = 0; i < maxUsers; i++) {
				if (userDisplayed.contains(new Integer(j))) {
					if (displayX)
						dataset.addSeries(series[i][0][j]);
					if (displayY)
						dataset.addSeries(series[i][1][j]);
					if (displayZ)
						dataset.addSeries(series[i][2][j]);
				}
			}
		}
	}

	@Override
	public void setDisplayX(boolean display) {
		displayX = display;
		// TODO update collection
	}

	@Override
	public void setDisplayY(boolean display) {
		displayY = display;
		// TODO update collection
	}

	@Override
	public void setDisplayZ(boolean display) {
		displayZ = display;
		// TODO update collection
	}

}
